package com.xinchuang.mapper.targetSystem.config;

import com.xinchuang.entity.targetSystem.config.TargetElse;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName TargetElseMapper
 * @Description: TODO(写一段描述的话)
 * @Author zhangxuan
 * @Date 2022-08-09 17:24
 * @Verion V1.0
 */
public interface TargetElseMapper {

    @Insert(value = {
            "INSERT INTO target_else (id, target_id, sub_id, name, address, status, remark, message, type) VALUES (#{id},#{targetId},#{subId},#{name},#{address},#{status},#{remark},#{message},#{type})"
    })
    int addTargetElse(TargetElse targetElse);

    @Update(value = {
            "<script>",
            "UPDATE target_else ",
            "<set>",
            "<if test = 'name != null'>name = #{name},</if>",
            "<if test = 'address != null'>address = #{address},</if>",
            "remark = #{remark},",
            "<if test = 'status != null'>status = #{status},</if>",
            "<if test = 'message != null'>message = #{message},</if>",
            "</set>",
            "WHERE id = #{id}",
            "<if test = 'subId != null'> AND sub_id = #{subId}</if>",
            "</script>"
    })
    int updateTargetElse(TargetElse targetElse);

    @Select(value = {
            "SELECT id, target_id, name, address, status, remark, message, type FROM target_else WHERE target_id = #{targetId} AND type = #{type} LIMIT 1"
    })
    @Results(value = {
            @Result(property = "targetId", column = "target_id")
    })
    TargetElse selectTargetElse(@Param("targetId") String targetId, @Param("type")Integer type);

    @Select(value = {
            "SELECT id, target_id, name, address, status, remark, message, type FROM target_else WHERE id = #{id} LIMIT 1"
    })
    @Results(value = {
            @Result(property = "targetId", column = "target_id")
    })
    TargetElse selectTargetElseById(@Param("id") String id);

    @Select(value = "SELECT id, target_id, name, address, status, remark, message, type FROM target_else WHERE target_id = #{targetId} AND type = #{type} AND sub_id IS NULL")
    @Results(value = {
            @Result(property = "targetId", column = "target_id")
    })
    List<TargetElse> selectTargetElseByTargetIdAndType(String targetId, Integer type);

    @Select(value = {
            "SELECT te.id, te.target_id, te.name, te.address, it.icon_url AS status, te.remark, te.message, te.type ",
            "FROM target_else te ",
            "LEFT JOIN ",
            "icon_table it ",
            "ON ",
            "te.status = it.id ",
            "WHERE te.target_id = #{targetId} AND te.type = 3 AND te. message = #{message} AND te.sub_id IS NULL"
    })
    @Results(value = {
            @Result(property = "targetId", column = "target_id")
    })
    List<TargetElse> selectTargetElseIcon(String targetId, String message);

    @Select(value = "SELECT id, target_id, sub_id, name, address, status, remark, message, type FROM target_else WHERE target_id = #{targetId} AND sub_id = #{subId} AND type = #{type}")
    @Results(value = {
            @Result(property = "targetId", column = "target_id"),
            @Result(property = "subId", column = "sub_id")
    })
    List<TargetElse> selectTargetElseByTargetIdAndTypeBySubId(String targetId, String subId, Integer type);

    @Select(value = {
            "SELECT te.id, te.target_id, te.name, te.address, it.icon_url AS status, te.remark, te.message, te.type ",
            "FROM target_else te ",
            "LEFT JOIN ",
            "icon_table it ",
            "ON ",
            "te.status = it.id ",
            "WHERE te.target_id = #{targetId} AND te.type = 3 AND te. message = #{message} AND te.sub_id = #{subId}"
    })
    @Results(value = {
            @Result(property = "targetId", column = "target_id")
    })
    List<TargetElse> selectTargetElseSubIcon(String targetId, String subId, String message);

    @Select(value = "SELECT COUNT(1) FROM target_else WHERE type = #{type}")
    int selectTypeAll(Integer type);

    @Select(value = {
            "SELECT name,COUNT(1) AS value FROM target_else WHERE type = #{type} GROUP BY name"
    })
    List<Map<String, Object>> selectTypeStat(Integer type);

    @Select(value = {
            "SELECT name,COUNT(1) AS value FROM target_else WHERE type = #{type} ",
            "AND target_id IN (SELECT target_id FROM system_relation WHERE s_id  = #{id}) ",
            "GROUP BY name"
    })
    List<Map<String, Object>> selectTypeStatSystem(Integer type, String id);

    @Select(value = {
            "SELECT name,COUNT(1) AS value FROM target_else WHERE type = #{type} ",
            "AND target_id IN (SELECT target_id FROM systems_relation WHERE sys_id  = #{id}) ",
            "GROUP BY name"
    })
    List<Map<String, Object>> selectTypeStatSystems(Integer type, String id);

    @Delete(value = "DELETE FROM target_else WHERE id = #{id}")
    int deleteTargetElse(String id);

    @Delete(value = "DELETE FROM target_else WHERE target_id = #{targetId}")
    int deleteTargetElseByTargetId(String targetId);

    @Delete(value = "DELETE FROM target_else WHERE sub_id = #{subId}")
    int deleteTargetElseBySubId(String subId);

}
